<template>
  <div>
    <el-row>
      <el-col :span="24" style="margin:5px 0">
        <el-button @click="previousPage" style="float:right; " type="success" plain icon="el-icon-arrow-left">
          返回</el-button>
      </el-col>
    </el-row>
    <el-form ref="form" :model="infoForm" label-width="150px" style="width:80%">
      <el-form-item label="策略名称">
        <el-input v-model="infoForm.name"></el-input>
      </el-form-item>
      <div v-if="formName == 'discBurning'">
        <el-form-item label="开启审计">
          <el-radio-group v-model="infoForm.disable" disabled >
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <div v-if="formName =='printingBehavior'">
        <el-form-item label="审计内容">
          <el-checkbox-group v-model="infoForm.content" disabled >
            <el-checkbox label="打印行为监控"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </div>
      <div v-if="formName=='fileDirectory'">
        <el-form-item label="文件和目录监控规则">
          <el-button type="text" icon="el-icon-plus" @click="addtableClick">新增</el-button>
          <el-table :data="tableData" border height="200">
            <el-table-column prop="name" label="文件/目录" align="center"></el-table-column>
            <el-table-column label="操作" width="120" align="center">
              <template slot-scope="scope">
                <el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="文件操作类型">
          <el-checkbox-group v-model="infoForm.fileType">
            <el-checkbox label="1" disabled>创建</el-checkbox>
            <el-checkbox label="2" >打开</el-checkbox>
            <el-checkbox label="3" disabled>修改</el-checkbox>
            <el-checkbox label="4" disabled>重命名</el-checkbox>
            <el-checkbox label="5" disabled>删除</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </div>
    </el-form>
    <div style="text-align:center">
      <el-button @click="previousPage">取 消</el-button>
      <el-button type="primary" @click="confirm">确 认</el-button>
    </div>
    <el-dialog :title="dialogtitle" :visible.sync="dialogVisible" width="40%">
      <el-row>
        <el-form label-position="right" label-width="100px" :model="tableform">
          <el-col :span="20">
            <el-form-item label="名称">
              <el-input size="mini" v-model="tableform.name"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmit" size="mini">确 认</el-button>
        <el-button size="mini" @click="dialogVisible=false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      infoForm: {
        name: '',
        disable: '0',
        content: ['打印行为监控'],
        fileType: ['1',  '3', '4', '5']
      },
      formName: '',
      tableData: [],
      dialogVisible: false,
      dialogtitle: '',
      tableform: {},
      formType: '',
      form: {
        name: '',
        creator: sessionStorage.getItem('userName'),
        enableTime: '2022-09-05T17:24:00',
        state: '1',
        Root: {
          Policy: {
            Type: '',
            Disable: '0',
            Name: '',
            FileMonitor: {
              Path: []
            },
            PrivateConfig: [],
          }
        }
      },

    }
  },
  created() {
    this.formName = this.$route.params.formName
    console.log(this.formName);
    if (this.$route.params.row) {
      this.form = this.$route.params.row;
      this.infoForm.name = this.form.name;
      console.log(this.form, 789)
      if (this.formName == 'fileDirectory') {
        this.form.Path.forEach(item => {
          console.log(item.Text)
          this.tableData.push({ name: item.Text });
        })
        console.log(this.tableData, 70)
        //this.tableData.push(this.formName.Path.Text)
        let arr = this.form.PrivateConfig[0].Operate;

        if(arr.open == 1){
            console.log(arr.open,689 )
          //this.infoForm.fileType[1] = arr.open;
          this.infoForm.fileType.push('2');
        }
        console.log(this.infoForm.fileType,'fileType')
        //this.infoForm.fileType[0] = arr.Create;
        //this.infoForm.fileType[2] = arr.Change;
        //this.infoForm.fileType[3] = arr.Rename;
        //this.infoForm.fileType[4] = arr.Delete;

      }
    }
  },
  methods: {
    //返回上一页
    previousPage() {
      this.$router.go(-1)
    },
    // 光盘刻录
    confirm() {
      var url = '';
      this.form.Root =
      {
        Policy:
        {
          Name: this.infoForm.name,
          Type: '',
          Disable: '0',
          FileMonitor: {
            Path: []
          },
          PrivateConfig: [],
        }
      };
      //this.form.name = this.infoForm.name;
      this.form.creator = 'secadm';
      if (this.formName == 'discBurning') {
        url = 'sjrz/policy/brasero'
        this.form.Root.Policy.Type = "brasero";
        this.form.Root.Policy.disable = this.infoForm.disable
      }
      if (this.formName == 'printingBehavior') {
        url = 'sjrz/policy/print'
        this.form.Root.Policy.Type = 'print'
      }
      if (this.formName == 'fileDirectory') {
        url = 'sjrz/policy/filemonitor'
        this.form.Root.Policy.Type = 'filemonitor'
        let Monitor = []
        this.tableData.forEach(item => {
          Monitor.push({ Text: item.name })
        })
        this.form.Root.Policy.FileMonitor.Path = Monitor;
        //文件操作类型
        let Operate = {};
        console.log(this.infoForm.fileType.indexOf('2'),'fileType')
        Operate = {
          Create: '1',
          open: this.infoForm.fileType.indexOf('2')=='-1' ? '0':'1',
          Change: '1',
          Rename: '1',
          Delete: '1',
        }
        this.form.Root.Policy.PrivateConfig.push({Operate:Operate});

      }
      this.$postForJson(url, { policy: this.form }).then(res => {
        let { data } = res
        if (data.errorcode == '0') {
          this.$message({
            type: "success",
            message: "提交成功！",
          });
          this.$router.go(-1)
        }
      })
    },
    //删除按钮
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    //新增按钮
    addtableClick() {
      this.dialogtitle = "新增"
      this.dialogVisible = true
    },
    //页面提交
    onSubmit() {
      this.dialogVisible = false
      this.tableData.push(this.tableform)
      this.tableform = {}
    },
  }
}
</script>

<style scoped>
/* * {
  padding: 0;
  margin: 0;
} */
</style>